package org.kl.bpm.web.conf;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kl.bf.entity.easyui.EasyuiGrid;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.kl.bpm.entity.conf.BpmConfBase;
import org.kl.bpm.entity.conf.BpmConfNode;
import org.kl.bpm.service.conf.BpmConfBaseService;
import org.kl.bpm.service.conf.BpmConfNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;
import org.springside.modules.web.Servlets;

/**
 * Activiti流程定义列表
 * 
 * @Author hqm
 * @CreateTime 2014年5月26日 下午5:15:45
 */
@Controller
@RequestMapping(value = "/bpm/confBase")
public class BpmConfBaseController extends BasicController {
	@Autowired
	private BpmConfBaseService bpmConfBaseService;
	@Autowired
	private BpmConfNodeService bpmConfNodeService;

	@RequiresPermissions("bpmConfBase:list")
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "bpm/bpmConfBaseList";
	}

	@RequiresPermissions("bpmConfBase:list")
	@RequestMapping(value = "dataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		Page<BpmConfBase> bpmConfBases = bpmConfBaseService.getUserBpmConfBase(searchParams, pageNumber, pageSize, sortType, orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(bpmConfBases.getContent());
		grid.setTotal((int) bpmConfBases.getTotalElements());
		String result = jsonMapper.toJson(grid);
		return result;
	}

	@RequiresPermissions("bpmConfBase:list")
	@RequestMapping(value = "nodeDataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String nodeDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = "20") int pageSize,
			@RequestParam(value = "sort", defaultValue = "priority") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		String bpmConfBaseId = request.getParameter("search_EQ_bpmConfBase.id");
		if (StringUtils.isBlank(bpmConfBaseId)) {
			return ajaxMsg("获取节点失败", false, null);
		}
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, SEARCH_PREFIX);
		Page<BpmConfNode> bpmConfNodes = bpmConfNodeService.getUserBpmConfNode(searchParams, pageNumber, pageSize, sortType, orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(bpmConfNodes.getContent());
		grid.setTotal((int) bpmConfNodes.getTotalElements());
		String result = jsonMapper.toJson(grid);
		return result;
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getBpmConfBase(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("bpmConfBase", bpmConfBaseService.getBpmConfBase(id));
		}
	}
}
